package com.youlai.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.common.model.Option;
import com.youlai.system.converter.GradeConverter;
import com.youlai.system.mapper.SysGradeMapper;
import com.youlai.system.model.entity.SysGrade;
import com.youlai.system.model.entity.SysUser;
import com.youlai.system.model.form.GradeForm;
import com.youlai.system.model.query.GradePageQuery;
import com.youlai.system.model.vo.GradePageVO;
import com.youlai.system.service.SysGradeService;
import com.youlai.system.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 职级业务接口实现
 */
@Service
@RequiredArgsConstructor
public class SysGradeServiceImpl extends ServiceImpl<SysGradeMapper, SysGrade> implements SysGradeService {

    private final GradeConverter gradeConverter;

    private final SysUserService userService;

    @Override
    public SysGrade getByContentEqual(String content) {
        LambdaQueryWrapper<SysGrade> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysGrade::getName, content);
        return getOne(queryWrapper);
    }

    @Override
    public Long saveByContent(String content) {
        SysGrade grade = new SysGrade();
        grade.setName(content);
        save(grade);
        return grade.getId();
    }

    @Override
    public Page<GradePageVO> getGradePage(GradePageQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        String keywords = queryParams.getKeywords();

        LambdaQueryWrapper<SysGrade> queryWrapper = new LambdaQueryWrapper<SysGrade>()
                .and(StrUtil.isNotBlank(keywords),
                        wrapper ->
                                wrapper.like(StrUtil.isNotBlank(keywords), SysGrade::getName, keywords)
                                        .or()
                                        .like(StrUtil.isNotBlank(keywords), SysGrade::getCode, keywords)
                );
        //查询数据
        Page<SysGrade> gradePage = this.page(new Page<>(pageNum, pageSize), queryWrapper);
        //设置人员数量和人员列表
        Map<Long, SysUser> gradeUserCountMap = userService.gradeUserCountMap();
        gradePage.getRecords().forEach(grade -> {
            SysUser user = gradeUserCountMap.get(grade.getId());
            if (user != null) {
                grade.setUserCount(user.getCount());
                grade.setUserList(user.getGroupNickName());
            }
        });
        return gradeConverter.entity2Page(gradePage);
    }

    @Override
    public boolean saveGrade(GradeForm gradeForm) {
        String name = gradeForm.getName();
        long nameCount = this.count(new LambdaQueryWrapper<SysGrade>().eq(SysGrade::getName, name));
        Assert.isTrue(nameCount == 0, "职级名称已存在");

        String code = gradeForm.getCode();
        long codeCount = this.count(new LambdaQueryWrapper<SysGrade>().eq(SysGrade::getCode, code));
        Assert.isTrue(codeCount == 0, "职级编号已存在");

        // 实体转换
        SysGrade grade = gradeConverter.form2Entity(gradeForm);
        return save(grade);
    }

    @Override
    public boolean updateGrade(Long gradeId, GradeForm gradeForm) {
        String name = gradeForm.getName();
        long nameCount = this.count(new LambdaQueryWrapper<SysGrade>()
                .eq(SysGrade::getName, name)
                .ne(SysGrade::getId, gradeId)
        );
        Assert.isTrue(nameCount == 0, "职级名称已存在");

        String code = gradeForm.getCode();
        long codeCount = this.count(new LambdaQueryWrapper<SysGrade>()
                .eq(SysGrade::getCode, code)
                .ne(SysGrade::getId, gradeId));
        Assert.isTrue(codeCount == 0, "职级编号已存在");

        // form -> entity
        SysGrade entity = gradeConverter.form2Entity(gradeForm);

        // 修改职级
        return this.updateById(entity);
    }

    @Override
    public GradeForm getGradeForm(Long gradeId) {
        SysGrade entity = this.getById(gradeId);
        return gradeConverter.entity2Form(entity);
    }

    @Override
    public boolean deleteGrades(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "职级删除数据为空");
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(ids);
    }

    @Override
    public boolean deleteGrades(List<Long> idList) {
        return this.removeByIds(idList);
    }

    @Override
    public List<Option> listGradeOptions() {
        // 查询数据
        List<SysGrade> gradeList = this.list(new LambdaQueryWrapper<SysGrade>()
                .select(SysGrade::getId, SysGrade::getName)
                .orderByAsc(SysGrade::getSort)
        );

        // 实体转换
        return gradeConverter.entities2Options(gradeList);
    }
}
